package com.pan;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * -ExcelToHtml
 * <p>
 * By:panpan.wang 2019年11月28日
 */
public class ExcelToHtml {
    public static void excelToHtml(String xlsfile, String htmlfile) {
        ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word
        try {
            app.setProperty("Visible", new Variant(false));
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            Dispatch excel = Dispatch.invoke(
                    excels,
                    "Open",
                    Dispatch.Method,
                    new Object[]{xlsfile, new Variant(false),
                            new Variant(true)}, new int[1]).toDispatch();
            Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[]{
                    htmlfile, new Variant(44)}, new int[1]);
            Variant f = new Variant(false);
            Dispatch.call(excel, "Close", f);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            app.invoke("Quit", new Variant[]{});
        }
        System.out.println("转换成功！");
    }

    public static void main(String[] args) {
        String paths = new String("D:/wangpanpan/杂货铺/2019年静安区中小学校、中职校实验室危险源明细表.xls");
        String savepaths = new String("D:/wangpanpan/杂货铺/生成html文件/text1.resultEXCEL.html");
        excelToHtml(paths, savepaths);
    }
}
